import { userLogin } from '@/api/user.js'
import { setToken, getToken } from '@/utils/storage.js'
export default {
  // 命名空间
  namespaced: true,
  // 1.state存储数据
  state: {
    token: getToken()
  },
  // 2.mutations:同步修改state数据
  mutations: {
    updateToken (state, payload) {
      state.token = payload
      // 同步到localStorage
      setToken(payload)
    }
  },
  // 3.actions:异步操作
  actions: {
    async getToken (context, payload) {
      // (1)发送ajax
      // const { data } = await userLogin(payload)
      // 响应拦截器结构data了💎💎💎💎💎💎💎💎💎💎💎💎
      const data = await userLogin(payload)
      console.log(data) // {code: 10000, message: '请求成功', data: {…}}
      // (2)调用commit 提交mutations
      context.commit('updateToken', data.data.token)
    }
  }

}
